package org.gurms.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.gurms.common.util.EncryptUtil;
import org.gurms.common.util.FormatUtil;
import org.gurms.common.util.ReflectionUtil;
import org.gurms.dao.po.system.SysUser;
import org.gurms.dao.po.system.SysUserInfo;
import org.gurms.dao.system.SysUserDao;
import org.gurms.dao.system.SysUserInfoDao;
import org.gurms.service.PojoConvert;
import org.gurms.service.dto.Page;
import org.gurms.service.dto.PropertyFilter;
import org.gurms.service.dto.system.UserDto;
import org.gurms.service.system.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysUserServiceImpl implements SysUserService{

	private Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);

	@Autowired
	private SysUserDao sysUserDao;

	@Autowired
	private SysUserInfoDao sysUserInfoDao;
	
	public SysUserDao getSysUserDao() {
		return sysUserDao;
	}

	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}

	public SysUserInfoDao getSysUserInfoDao() {
		return sysUserInfoDao;
	}

	public void setSysUserInfoDao(SysUserInfoDao sysUserInfoDao) {
		this.sysUserInfoDao = sysUserInfoDao;
	}

	@Transactional(readOnly = true)
	public boolean isExist(String userid) {
		SysUser user = sysUserDao.get(userid);
		return user == null ? false : true;
	}

	@Transactional(readOnly = true)
	public UserDto get(UserDto userDto){
		SysUser user = sysUserDao.get(userDto.getUserid());
		return PojoConvert.convert(user);
//		SysUserInfo userinfoPo = sysUserInfoDao.get(userDto.getUserid());
//		UserDto user = new UserDto();
//		return getUserDtoFromPo(userPo, userinfoPo);
	}
	
	public void save(UserDto userDto) {
		SysUser userPo = getSysUserFromDto(userDto);
		String pw = EncryptUtil.md5Encode(userDto.getLoginpassword());
		userPo.setLoginpassword(pw);
		userPo.setLogintime(FormatUtil.getCurrentTime());
		sysUserDao.save(userPo);
		
		SysUserInfo userinfoPo = getSysUserInfoFromDto(userDto);
		sysUserInfoDao.save(userinfoPo);
	}

	public void update(UserDto userDto) {
		SysUser po = getSysUserFromDto(userDto);
		String pw = EncryptUtil.md5Encode(userDto.getLoginpassword());
		po.setLoginpassword(pw);
		sysUserDao.update(po);

		SysUserInfo userinfoPo = getSysUserInfoFromDto(userDto);
		sysUserInfoDao.save(userinfoPo);
	}

	public void delete(UserDto userDto) {
		sysUserDao.delete(userDto.getUserid());
		sysUserInfoDao.delete(userDto.getUserid());
	}

	@Transactional(readOnly = true)
	public Page<UserDto> getAll(Page<UserDto> page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(readOnly = true)
	public Page<UserDto> search(Page<UserDto> pageDto, List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		Page<SysUser> page = new Page<SysUser>();
		ReflectionUtil.getDozer().map(pageDto, page);
		page = sysUserDao.findPage(page, filters);
		
		List<SysUser> poList = page.getResult();
		List<UserDto> dtoList = new ArrayList<UserDto>();
		for(SysUser po : poList){
			dtoList.add((UserDto)ReflectionUtil.getDozer().map(po, UserDto.class));
		}
		pageDto.setResult(dtoList);
		return pageDto;
	}
	
	private SysUser getSysUserFromDto(UserDto dto){
		SysUser user = (SysUser)ReflectionUtil.getDozer().map(dto, SysUser.class);
		user.setErrorcount(1);
		user.setLogincount(2);
		user.setOnlineflag("1");
		user.setUserstatus("1");
		return user;
	}
	
	private SysUserInfo getSysUserInfoFromDto(UserDto dto){
		return(SysUserInfo)ReflectionUtil.getDozer().map(dto, SysUserInfo.class);
	}
	
}
